package com.chromatic.competition.view.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.chromatic.competition.R;
import com.chromatic.competition.service.SendSmsCodeService;
import com.chromatic.competition.service.UserServiceImpl;

public class RegisterActivity extends AppCompatActivity {
    private EditText usernameEditText;
    private EditText phoneNumberEditText;
    private EditText passwordEditText;
    private EditText confirmPasswordEditText;
    private EditText smsVerificationCodeEditText;
//    private EditText invitationCodeEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

         usernameEditText = findViewById(R.id.usernameEditText);
         phoneNumberEditText = findViewById(R.id.phoneNumberEditText);
         smsVerificationCodeEditText = findViewById(R.id.smsVerificationCodeEditText);
         passwordEditText = findViewById(R.id.passwordEditText);
         confirmPasswordEditText = findViewById(R.id.confirmPasswordEditText);
//         invitationCodeEditText = findViewById(R.id.invitationCodeEditText);
        Button sendVerificationCodeButton = findViewById(R.id.sendVerificationCodeButton);
        Button registerButton = findViewById(R.id.registerButton);
        Button backToLoginButton = findViewById(R.id.back_to_login_btn);

        // Set click listeners for buttons
        sendVerificationCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = phoneNumberEditText.getText().toString().trim();
                SendSmsCodeService.sendSmsCode(RegisterActivity.this, phoneNumber);
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditText.getText().toString().trim();
                String phoneNumber = phoneNumberEditText.getText().toString().trim();
                String smsVerificationCode = smsVerificationCodeEditText.getText().toString();
                String password = passwordEditText.getText().toString().trim();
                String confirmPassword = confirmPasswordEditText.getText().toString().trim();
                UserServiceImpl.register(RegisterActivity.this, username, phoneNumber,
                        password, confirmPassword, smsVerificationCode);
                Toast.makeText(RegisterActivity.this, "注册", Toast.LENGTH_SHORT).show();
            }
        });

        backToLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个 Intent 对象，跳转到登录界面
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                // 结束当前 Activity
                finish();
            }
        });
    }
}